package java课题设计.功能类;
 
import java课题设计.工具类.RandomID;
 
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDate;
import java.util.Random;
 
public class 学生数据写入文件 {
    private static final int NUM_OF_STUDENTS = 100000;
    private JTable table;
 
    public 学生数据写入文件() {
        // 设置界面风格
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException |
                 UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }
 
 
        ranstudent();
    }
 
    public void ranstudent() {
        // 创建表格
        String[] columnNames = {"学生姓名","性别", "学号", "体育成绩", "Java成绩", "高数成绩", "出生年月日"};
        DefaultTableModel model = new DefaultTableModel(columnNames, NUM_OF_STUDENTS);
        table = new JTable(model);
 
 
        // 创建一个 FileWriter 和一个 BufferedWriter 对象，用于将数据写入文件
        try (FileWriter fw = new FileWriter("D:\\aaa\\数据\\学生数据.txt", true);
             BufferedWriter bw = new BufferedWriter(fw)) {
            // 写入文件标题
            bw.write("学生姓名\t性别\t学号\t体育成绩\tJava成绩\t高数成绩\t出生年月日");
            bw.newLine();
 
            // 生成学生数据并写入文件
            Random random = new Random();
            for (int i = 0; i < NUM_OF_STUDENTS; i++) {
                RandomID randomID = new RandomID();
                String sex;
                String name = "学生" + (i + 1);
                int a=random.nextInt(2);
                if(a==0){
                    sex="男";
                }
                else sex="女";
                int id = randomID.getid();
                double PEscore = randomID.getgrade();
                double Javascore = randomID.getgrade();
                double Mathscore = randomID.getgrade();
                LocalDate birthDate = LocalDate.of(2008 - random.nextInt(5), random.nextInt(12) + 1, random.nextInt(28) + 1);
 
                // 将学生数据写入文件
                bw.write(name + "\t"+ sex +"\t" + id + "\t" + String.format("%.0f", PEscore) + "\t" + String.format("%.0f", Javascore) + "\t" + String.format("%.0f", Mathscore) + "\t" + birthDate.toString());
                bw.newLine();
                //将学生数据添加到表格中
                Object[] rowData = {name,sex, id, String.format("%.0f", PEscore), String.format("%.0f", Javascore), String.format("%.0f", Mathscore), birthDate};
                model.setValueAt(name, i, 0);
                model.setValueAt(sex,i,1);
                model.setValueAt(id, i, 2);
                model.setValueAt(String.format("%.0f", PEscore), i, 3);
                model.setValueAt(String.format("%.0f", Javascore), i, 4);
                model.setValueAt(String.format("%.0f", Mathscore), i, 5);
                model.setValueAt(birthDate, i, 6);
            }
            System.out.println("数据已经成功写入文件！");
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 创建 JScrollPane
        JScrollPane scrollPane = new JScrollPane(table);
 
        // 创建 JFrame 并展示 UI 界面
        JFrame frame = new JFrame("学生信息表");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
 
        JPanel contentPane = new JPanel(new BorderLayout());
        contentPane.add(scrollPane, BorderLayout.CENTER);
        frame.setContentPane(contentPane);
 
        frame.pack();
        frame.setVisible(true);
    }
}